package com.telit.common.response;


import java.io.Serializable;


/**
 * Create by Chencf on 2022/07/11
 * 定义接口返回格式
 */
public class Result<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    /** 响应代码 */
    private Integer code;
    /** 响应描述信息 */
    private String errorMsg;
    /** 响应数据 */
    private T data;


    // 默认构造
    public Result() {
        this.code = CommonResultCode.SUCCESS.getCode();
        this.errorMsg = CommonResultCode.SUCCESS.getErrorMsg();
    }


    //getter setter
    public T getData() {
        return data;
    }
    public void setData(T data) {
        this.data = data;
    }
    public Integer getCode() {
        return code;
    }
    public void setCode(Integer code) {
        this.code = code;
    }
    public String getErrorMsg() {
        return errorMsg;
    }
    public void setMsg(String msg) {
        this.errorMsg = msg;
    }


    @Override
    public String toString() {
        return "Result{" +
                "code=" + code +
                ", errorMsg='" + errorMsg + '\'' +
                ", data=" + data +
                '}';
    }
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Result<?> result = (Result<?>) o;
        if (code != null ? !code.equals(result.code) : result.code != null) return false;
        if (errorMsg != null ? !errorMsg.equals(result.errorMsg) : result.errorMsg != null) return false;
        return data != null ? data.equals(result.data) : result.data == null;
    }
    @Override
    public int hashCode() {
        int result = code != null ? code.hashCode() : 0;
        result = 31 * result + (errorMsg != null ? errorMsg.hashCode() : 0);
        result = 31 * result + (data != null ? data.hashCode() : 0);
        return result;
    }


    // build  一般用来返回错误信息
    public static <T> Result<T> build(String msg) {
        return build(CommonResultCode.ERR.getCode(), msg, null);
    }
    public static <T> Result<T> build(ResultCode code) {
        return build(code.getCode(), code.getErrorMsg(), null);
    }
    // 当第二个参数为String或null时，会走这个重载
    public static <T> Result<T> build(ResultCode code, String msg) {
        return build(code.getCode(), msg, null);
    }
    // 当第二个参数，非String，非null时，才会走这个重载
    public static <T> Result<T> build(ResultCode code, T data) {
        return build(code.getCode(), code.getErrorMsg(), data);
    }
    public static <T> Result<T> build(ResultCode code, String msg, T data) {
        return build(code.getCode(), msg, data);
    }

    public static <T> Result<T> build(int code, String msg) {
        return Result.build(code, msg, null);
    }

    // Base
    public static <T> Result<T> build(int code, String msg, T data) {
        Result<T> response = new Result<T>();
        response.setCode(code);
        response.setMsg(msg);
        response.setData(data);
        return response;
    }


    //success 一般用来返回成功信息
    public static <T> Result<T> success() {
        return build(CommonResultCode.SUCCESS.getCode(), CommonResultCode.SUCCESS.getErrorMsg(), null);
    }
    public static <T> Result<T> success(T data) {
        return build(CommonResultCode.SUCCESS.getCode(), CommonResultCode.SUCCESS.getErrorMsg(), data);
    }
}